<template>
  <div>
    <!-- 面包屑导航区 -->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card>
        <el-button type="primary">添加角色</el-button>

        <el-table :data="rolesList" stripe>
            <el-table-column type="expand">
                <template slot-scope="scope">
                    <el-row  :class="['bdbottom',i1 === 0 ? 'bdtop':'','vcenter']" v-for="(item1,i1) in scope.row.children" :key="item1.id">
                        <!-- 渲染一级权限 -->
                        <el-col :span="5">
                            <el-tag  >{{ item1.authName}}</el-tag>
                            <i class="el-icon-caret-right"></i>
                        </el-col>
                        <!-- 渲染二级和三级权限 -->
                        <el-col :span="19">
                            <el-row :class="['bdtop',i2 === 0 ? '':'bdtop','vcenter']"  v-for="(item2,i2) in item1.children" :key="item2.id">
                                <el-col :span="6">
                                     <el-tag  type="success">{{ item2.authName }}</el-tag>
                                     <i class="el-icon-caret-right"></i>
                                </el-col >
                                <el-col :span="18">
                                    <el-tag closable type="danger" v-for="item3 in item2.children" :key="item3.id"
                                     @close="deletRights(scope.row,item3.id)">{{ item3.authName }}</el-tag>
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="角色名称" prop="roleName"></el-table-column>
            <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
            <el-table-column>
                <template slot-scope="scope">
                    <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                    <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
                    <el-button size="mini" type="warning" icon="el-icon-setting" @click="setRights(scope.row)">分配权限</el-button>
                </template>
            </el-table-column>
        </el-table>
      </el-card>
      <el-dialog
        title="分配权限"
        :visible.sync="setDialogVisible"
        width="50%"
        @close="setRoleClosed"
        >
        <el-tree :data="setRoleList" :props="defaultProps" ref="setRoleRef" show-checkbox 
        node-key="id"  default-expand-all :default-checked-keys="defKeys"
        >
        </el-tree>
        <span slot="footer" class="dialog-footer">
            <el-button @click="setDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="toSetRole">确 定</el-button>
        </span>
      </el-dialog>

  </div>
</template>

<script>
export default {
    created() {
        this.getRolesList()

        
    },
    data() {
        return {
            rolesList:[],
            setDialogVisible:false,
            setRoleList:[],
            defaultProps:{
                label:'authName',
                children:'children'
            },
            roleId:'',
            defKeys:[]
            
        }
    },
    methods: {
        async getRolesList(){
          const{data : res} = await this.$http.get('roles')
          this.rolesList = res.data
        },
        async deletRights(role,rightId){
         const confirmResult = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if(confirmResult !== 'confirm'){
            this.$message.info('已取消删除')
        }
        const {data : res} = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)        
        if(res.meta.status !== 200){
            this.$message.error('删除失败')
        }
           this.$message.success('删除成功')
           role.children = res.data
        },
        async setRights(role){
           this.roleId = role.id
           const{data : res} = await this.$http.get('rights/tree')
           if(res.meta.status !== 200){
             this.$message.error('获取权限列表失败')
           }
           this.setRoleList = res.data
           this.getDefKeys(role,this.defKeys)
           this.setDialogVisible = true
        },
        // 通过递归将三级节点中id全部加入到defKeys中
        getDefKeys(node,arr){
            // 如果是三级节点
            if(!node.children){
                return arr.push(node.id)
            }
            node.children.forEach(item =>
                this.getDefKeys(item,arr))
        },
        setRoleClosed(){
            this.defKeys = []
        },
        async toSetRole(){
            let keys = [
                ...this.$refs.setRoleRef.getCheckedKeys(),
                ...this.$refs.setRoleRef.getHalfCheckedKeys()
            ]
            const strKeys = keys.join(',')
            const {data : res} = await this.$http.post(`roles/${this.roleId}/rights`,{rids:strKeys})
            if(res.meta.status !== 200){
                this.$message.error('分配权限失败')
            }
            this.$message.success('分配权限成功')
            this.getRolesList()
            this.setDialogVisible = false
            
            
        }
    },

}
</script>

<style lang="less" scoped>
.el-tag{
    margin: 7px;
    margin-left: 30px;
}
.bdtop{
    border-top: 1px solid #eee;
}
.bdbottom{
    border-bottom: 1px solid #eee;
}
.vcenter{
    display: flex;
    align-items: center;
}
</style>